<?php
/**
 * 收藏模块控制器
 *
 * 收藏模块controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\FavoritesService\FavoritesService;
use Illuminate\Support\Facades\Response;

class FavoritesController extends Controller
{
    protected $favoritesService;
    public function __construct(FavoritesService $favoritesService)
    {
        $this->favoritesService = $favoritesService;
    }

    /**
     * 添加收藏
     * @param Request $request
     * @return mixed
     */
    public function postAdd(Request $request){
        $check_data = array(
            'resource_id'=>'required|alpha_num',
            'user_id'=>'required|alpha_num',
            'module'=>'required|alpha_num'
        );
        $v = $this->favoritesService->store($request,$check_data);
        if ($v->fails())
        {
            if ($v->errors()->first('resource_id')) {
                $resultMes = $this->favoritesService->createResult('M11001',array('type'=>0));
            }elseif($v->errors()->first('user_id')){
                $resultMes = $this->favoritesService->createResult('M11002',array('type'=>0));
            }else{
                $resultMes = $this->favoritesService->createResult('M11003',array('type'=>0));
            }
            return Response::json($resultMes);
        }
        $user_id = $request->input('user_id');
        $resource_id = $request->input('resource_id');
        $module = $request->input('module');
        $result = $this->favoritesService->addCollect($user_id,$resource_id,$module);
        return Response::json($result);
    }

    /**
     * 获取用户收藏的列表
     * @param Request $request
     * @return mixed]
     */
    public function postGetFavorites(Request $request){
        $check_data = array(
            'user_id'=>'required|alpha_num',
            'per_page'=>'required|integer|min:1',
            'module'=>'required|alpha_num'
        );
        $v = $this->favoritesService->store($request,$check_data);
        if ($v->fails())
        {
            if ($v->errors()->first('user_id')) {
                $resultMes = $this->favoritesService->createResult('M11002',(object)array());
            }elseif($v->errors()->first('per_page')){
                $resultMes = $this->favoritesService->createResult('M11004',(object)array());
            }else{
                $resultMes = $this->favoritesService->createResult('M11003',(object)array());
            }
            return Response::json($resultMes);
        }
        $user_id = $request->input('user_id');
        $per_page = $request->input('per_page');
        $module = $request->input('module');
        $result = $this->favoritesService->getFavorites($user_id,$module,$per_page);
        return Response::json($result);
    }

    /**
     * 获取某产品的收藏用户
     * @param Request $request
     * @return mixed
     */
    public function postGetFavoritesUsers(Request $request){
        $user_id = intval($request->input('user_id'));
        $resource_id = trim($request->input('resource_id'));
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $app_id = intval($request->input('app_id'));
        $result = $this->favoritesService->get_favorites_users($user_id,$resource_id,$app_id,$per_page);
        return Response::json($result);
    }
}
